<template>
	<view>
		<zmm-watermark :watermark="userStore.watermark" v-if="userStore.watermark"></zmm-watermark>
		<!--导航栏-->
		<pageLayout
			:barTitle="title"
			:isSearchNull="isSearchNull"
			:isOpen="isOpen"
			@rightClick="clickSearch">
		</pageLayout>
		<!--查询弹窗-->
		<up-popup :z-index="90" :show="isOpen" @close="closeSearch" mode="top">
			<scroll-view class="search" scroll-y="true" @touchmove.stop>
				<view class="search-form">
					<uni-forms ref="searchRef" :modelValue="searchModel" label-width="160rpx">
						<uni-forms-item label="优惠券名称">
							<uni-easyinput v-model="searchModel.name" placeholder="请输入优惠券名称" />
						</uni-forms-item>
						<uni-forms-item label="获得类型">
							<uni-data-select
								v-model="searchModel.type"
								placeholder="请选择获得类型"
								:localdata="allTypeList"></uni-data-select>
						</uni-forms-item>
						<uni-forms-item label="券状态">
							<uni-data-select
								v-model="searchModel.status"
								placeholder="请选择券状态"
								:localdata="allStatusList"></uni-data-select>
						</uni-forms-item>
						<uni-forms-item label="开始时间">
							<uni-datetime-picker type="date" placeholder="请选择开始时间" v-model="searchModel.startTime"/>
						</uni-forms-item>
						<uni-forms-item label="结束时间">
							<uni-datetime-picker type="date" placeholder="请选择结束时间" v-model="searchModel.endTime"/>
						</uni-forms-item>
					</uni-forms>
					<view class="search-footer main-flex">
						<up-button
							class="search-footer-item"
							text="搜索"
							type="primary"
							@click="searchSubmit"
							icon="search"></up-button>
						<up-button
							class="search-footer-item"
							@click="resetSubmit"
							text="重置"></up-button>
					</view>
				</view>
			</scroll-view>
		</up-popup>
		<view class="content">
			<!--内容-->
			<view class="table">
				<ty-table
					:data="tableList"
					:columns="columns"
					:sortable="true"
					:cellClassName="() => 'table-cell'"
					:headerCellClassName="() => 'table-cell'"
					:rowClassName="() => 'table-row'"
					stripe
					border>
					<template v-slot:user="{ row }">
						<view class="table-td-cell">
							<!-- <view class="table-td-row">ID：<text class="table-td-text">{{row.id}}</text></view> -->
							<view class="table-td-row">名称：<text class="table-td-text">{{row.couponName}}</text></view>
							<view class="table-td-row">用户：<text class="table-td-text">{{row.customerName}}</text></view>
						</view>
					</template>
					<template v-slot:coupon="{ row }">
						<view class="table-td-cell">
							<view class="table-td-row">类型：<text class="table-td-text">{{arrText(allTypeList, row.type)}}</text></view>
							<view class="table-td-row">状态：<text class="table-td-text" :class="`status${row.status}`">{{arrText(allStatusList, row.status)}}</text></view>
						</view>
					</template>
					<template v-slot:time="{ row }">
						<view class="table-td-cell">
							<view class="table-td-row">开始：<text class="table-td-text">{{row.startTime}}</text></view>
							<view class="table-td-row">结束：<text class="table-td-text">{{row.endTime}}</text></view>
							<view class="table-td-row">备注：<text class="table-td-text">{{row.remark}}</text></view>
						</view>
					</template>
					<template v-slot:empty>
						<view class="table-empty">
							<up-empty mode="data"/>
						</view>
					</template>
				</ty-table>
			</view>
			<!--分页-->
			<view class="pagination" v-show="total > 0">
				<ty-pagination
					:total="total"
					:pageSize="pageSize"
					:currentPage="pageNum"
					:pageSizes="[10, 20, 30, 50]"
					mode="simple"
					layout="first,prev,page,next,last"
					@sizeChange="sizeChange"
					@change="pageChange">
				</ty-pagination>
			</view>
		</view>
	</view>
</template>

<script setup>
import { onLoad } from '@dcloudio/uni-app';
import { computed, onMounted, ref } from 'vue';
import { tab, auth, modal } from "@/plugins"
import { getTitle } from "@/utils/getter"
import { checkPermi, checkRole, checkOnlyPermi, checkOnlyRole } from "@/utils/permission"
import { couponStatus, couponType } from "@/api/system/dict/data"
import { couponRecord } from "@/api/erp_api/coupon"
import { isObjectEmptySimple, copyText, arrText } from '@/utils/ty.js';
import useUserStore from '@/store/modules/user';

	const userStore = useUserStore()

	// 标题设置
	const title = ref('')
	const couponId = ref('')
	onLoad((option) => {
		title.value = getTitle(userStore.menu)
		uni.setNavigationBarTitle({
			title: title.value 
		})
		if (option.couponId) {
			couponId.value = option.couponId
		}
		if (option.name) {
			searchModel.value.name = option.name
		}
	})
	
	// 商品券类型
	const allTypeList = ref([])
	const getCouponType = () => {
		return couponType().then(res => {
			allTypeList.value = res.data.map(item => {
				return {
					...item,
					value: item.dictValue,
					text: item.dictLabel.slice(2)
				}
			}) || []
		})
	}
	
	// 商品券状态
	const allStatusList = ref([])
	const getCouponStatus = () => {
		return couponStatus().then(res => {
			allStatusList.value = res.data.map(item => {
				return {
					...item,
					value: item.dictValue,
					text: item.dictLabel
				}
			}) || []
		})
	}
	
	// 搜索相关
	const isSearchNull = ref(true)
	const isOpen =  ref(false)
	const clickSearch = (val) => {
		isOpen.value = val
	}
	const closeSearch = (val) => {
		isOpen.value = false
	}
	// 确认搜索
	const searchRef = ref(null)
	const searchModel = ref({
		type: '',
		status: '',
		startTime: '',
		endTime: '',
		name: ''
	})
	const searchSubmit = () => {
		pageNum.value = 1
		tableList.value = []
		isOpen.value = false
		searchFirst()
	}
	// 重置搜索
	const resetSubmit = () => {
		// searchRef.value.resetFields()
		searchModel.value = {
			type: '',
			status: '',
			startTime: '',
			endTime: '',
			name: ''
		}
		isSearchNull.value = true
		isOpen.value = false
		searchFirst()
	}
	
	
	// 数据查询
	const pageNum = ref(1)
	const pageSize = ref(10)
	const total = ref(0)
	const tableList = ref([])
	const isLoading = ref(false)
	
	// 表格头部
	const columns = ref([
		{ title: '用户信息', key: 'user' },
		{ title: '商品券信息', key: 'coupon', width: '200rpx' },
		{ title: '时间/备注', key: 'time' }
	])
	
	// 从第一页查询
	const searchFirst = () => {
		pageNum.value = 1
		tableList.value = []
		isSearchNull.value = isObjectEmptySimple(searchModel.value)
		getList()
	}
	//页数切换
	const pageChange = (page) => {
		pageNum.value = page
		getList()
	}
	//每页条数切换
	const sizeChange = (size) => {
		pageSize.value = size
		getList()
	}
	
	const getList = () => {
		const params = {
			pageNum: pageNum.value,
			pageSize: pageSize.value
		}
		if (searchModel.value.type || searchModel.value.type === 0) {
			params.type = searchModel.value.type
		}
		if (searchModel.value.status || searchModel.value.status === 0) {
			params.status = searchModel.value.status
		}
		if (searchModel.value.startTime) {
			params.startTime = searchModel.value.startTime
		}
		if (searchModel.value.endTime) {
			params.endTime = searchModel.value.endTime
		}
		if (searchModel.value.name) {
			params.name = searchModel.value.name
		}
		if (couponId.value) {
			params.couponId = couponId.value
		}
		isLoading.value = true
		uni.showLoading({
			title: '数据加载中...'
		})
		couponRecord(params).then(res => {
			tableList.value = res.rows || []
			total.value = res.total
			isLoading.value = false
			uni.hideLoading()
    }).catch(() => {
			isLoading.value = false
			uni.hideLoading()
		})
	}
	
	onMounted(async () => {
		await getCouponStatus()
		await getCouponType()
		searchFirst()
	})
</script>

<style scoped>
.status1 {
	color: #19be6b;
}
.status2 {
	color: #fa3534;
}
</style>
